//
//  MCLoginModel.m
//  TLYL
//
//  Created by miaocai on 2017/7/13.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCLoginModel.h"


@interface MCLoginModel (){
    //_userNamet 用户名
    NSString *_userNamet;
    //_passWord 密码
    NSString *_passWord;
}
//userId 用户id
@property (nonatomic,strong) NSString *userId;
//userName 用户名
@property (nonatomic,strong) NSString *userName;

@end

@implementation MCLoginModel

- (instancetype)initWithUserName:(NSString *)userName passWord:(NSString *)passWord{

    if (self = [super init]) {
        _userNamet = userName;
        _passWord = passWord;
    }
    return self;
    
}



- (NSDictionary<NSString *,NSString *> *)headerForManagerWithManager:(ApiBaseManager *)manager{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"cookie_id"]) {
       return @{@"token":@"",@"platformCode":@"5",@"userId":@"",@"apiid":[[NSUserDefaults standardUserDefaults] objectForKey:@"cookie_id"]};
    }else{
      return @{@"token":@"",@"platformCode":@"5",@"userId":@"",@"apiid":@""};
    }
}

- (NSString *)urlPathComponentForManagerWithManager:(ApiBaseManager *)manager{
    
    return @"web-api/api/v4/login";
}

- (NSDictionary<NSString *,id> *)parametersForManagerWithManager:(ApiBaseManager *)manager{
    
    NSString *timeString = [NSString getCurrentTimestamp];
    NSDictionary *dic = nil;
    _passWord = [_passWord MD5];
    if (self.authCode == nil&&self.authCode == nil) {
        dic =@{@"UserLoginName":_userNamet,@"UserPassWord":_passWord};
    }
    if (self.authCode != nil && self.GACode == nil) {
        dic =@{@"UserLoginName":_userNamet,@"UserPassWord":_passWord,@"SecCode":self.authCode};
    }
    if (self.authCode != nil && self.GACode != nil) {
        dic =@{@"UserLoginName":_userNamet,@"UserPassWord":_passWord,@"SecCode":self.authCode,@"GACode":self.GACode,@"GAKey":self.GAKey};
    }
    if (self.authCode == nil && self.GACode != nil) {
        dic =@{@"UserLoginName":_userNamet,@"UserPassWord":_passWord,@"GACode":self.GACode,@"GAKey":self.GAKey};
    }
    [[NSUserDefaults standardUserDefaults] setObject:_userNamet forKey:@"UserName"];
    NSString *jsonStr = [dic convertToJsonData];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",jsonStr,@"",timeString];
    return @{@"params":jsonStr,
             @"sign":[sign MD5],
             @"timestamp":timeString
             };
}

- (void)managerCallAPIDidSuccessWithManager:(ApiBaseManager *)manager{
 
    
    if (manager.ResponseHeader[@"token"]) {
        [[NSUserDefaults standardUserDefaults] setObject:manager.ResponseHeader[@"token"] forKey:@"token"];
    }
    if (manager.ResponseRawData[@"expires_in"]) {
    
      NSNumber *time =  manager.ResponseRawData[@"expires_in"];
      NSDate *date = [NSDate dateWithTimeIntervalSinceNow:time.intValue];
     [[NSUserDefaults standardUserDefaults] setObject:date forKey:@"expires_in"];
    }

    if (manager.ResponseRawData[@"userId"]) {
        [[NSUserDefaults standardUserDefaults] setObject:manager.ResponseRawData[@"userId"] forKey:@"userId"];
    }
    if (manager.ResponseRawData[@"UserId"]) {
        [[NSUserDefaults standardUserDefaults] setObject:manager.ResponseRawData[@"UserId"] forKey:@"userId"];
    }
    if (manager.ResponseRawData[@"userName"]) {
        [[NSUserDefaults standardUserDefaults] setObject:manager.ResponseRawData[@"userName"] forKey:@"userName"];
    }
    
    NSDictionary *dict = manager.ResponseHeader;
    NSString *str = dict[@"Set-Cookie"];
    if ([str containsString:@"HttpOnly,"] && str !=nil) {
        NSString *str1 = [str componentsSeparatedByString:@"HttpOnly,"][1];
        
        NSString *str2 =  [str1 componentsSeparatedByString:@"APIID="][1];
        [[NSUserDefaults standardUserDefaults] setObject:str2 forKey:@"cookie_id"];
    }else{
//        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"cookie_id"];
    }
    
    
   
//    [BKIndicationView dismiss];
    if (self.callBackSuccessBlock) {
        self.callBackSuccessBlock(manager.ResponseRawData);
    }
    
}

- (void)managerCallAPIDidFailedWithManager:(ApiBaseManager *)manager errorCode:(NSString *)errorCode{
//    [BKIndicationView dismiss];
    if (self.callBackFailedBlock) {
        self.callBackFailedBlock(manager, errorCode);
    }

}


@end
